﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Zero.Presenter;
using Zero.Presenter.Donnees;
using TypeMedia = Zero.POCO.Compagne.TypeMedia;

namespace DevExpress.MailClient.Win.Forms.Campagne
{
    public partial class frmEditPlan : XtraForm
    {
        public ModelViewPlan sourcePlan;
        private bool _newPlan;
        private Guid planId;
        private bool _isPlanModified;
        public frmEditPlan(ModelViewPlan plan,bool newPlan,String caption )
        {
            
            InitializeComponent();
            cbePlan.SelectedIndex = 0;
            DialogResult = DialogResult.Cancel;
            _newPlan = newPlan;
            if (_newPlan)
            {
                planId = default(Guid);
                _isPlanModified = true;
            }
            else
            {
                planId = plan.id;
                _isPlanModified = false;
            }
        }



        DialogResult QueryClose()
        {
            if (!_isPlanModified)
                return DialogResult.Yes;

            DialogResult result = XtraMessageBox.Show(this, Properties.Resources.SaveQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            switch (result)
            {
                case DialogResult.Cancel:
                {
                    return DialogResult.Cancel;
                }
                case DialogResult.No:
                    return DialogResult.No;
                default:
                    {
                        ApplyChanges();
                        return DialogResult.Yes;
                    }
            }
        }

        private void ApplyChanges()
        {
            sourcePlan=new ModelViewPlan();
            sourcePlan.id = planId;
            sourcePlan.TypeMedia = (TypeMedia) cbePlan.SelectedIndex;
            sourcePlan.Nom = teNom.Text;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            _isPlanModified = false;
            ApplyChanges();
            DialogResult=QueryClose();
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult=QueryClose();
        }

        private void teNom_EditValueChanged(object sender, EventArgs e)
        {
            _isPlanModified = true;
        }

        private void cbePlan_SelectedIndexChanged(object sender, EventArgs e)
        {
            _isPlanModified = true;
        }

        private void teNom_Validating(object sender, CancelEventArgs e)
        {
            if (teNom.Text.Trim().Length <= 0)
                e.Cancel = true;
        }
    }
}